<?php

/**
 * 
 * curl处理工具类
 *
 */
class CurlRequest
{
	const DEFAULT_METHOD = 'GET';
	const DEFAULT_TIMEOUT = 5;

	/**
	 * 执行一个HTTP请求
	 * @param string $url 请求URL
	 * @param array $params 请求的参数
	 * @param string $method 请求方法
	 * @param int $timeout 执行的超时时间
	 * @return string HTTP返回信息
	 */
	public static function makeRequest($url, $params = array(), $method = self::DEFAULT_METHOD, $cookie_file = '', $curlopt = array()){
		$method = strtoupper($method);
		$is_ssl = substr($url, 0, 8) == 'https://' ? TRUE : FALSE;
		$ch = curl_init();
		$opt = array();
		$opt[CURLOPT_HEADER] = FALSE;
		$opt[CURLOPT_RETURNTRANSFER] = TRUE;
		$opt[CURLOPT_TIMEOUT] = self::DEFAULT_TIMEOUT;
		if ($cookie_file){
// 		curl_setopt($ch, CURLOPT_COOKIE, $cookie);
			curl_setopt($ch, CURLOPT_COOKIEFILE, $cookie_file);
			curl_setopt($ch, CURLOPT_COOKIEJAR, $cookie_file);
		}
		if ($curlopt) {
			foreach ($curlopt as $o=>$val)
			{
				curl_setopt($ch, $o, $val);
			}
		}
		if ($is_ssl){
			$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
			$opt[CURLOPT_SSL_VERIFYHOST] = FALSE;
		}
		if ($method == 'POST'){
			$opt[CURLOPT_POST] = TRUE;
			$opt[CURLOPT_POSTFIELDS] = $params;
			$opt[CURLOPT_URL] = $url;
		}else{
			$queryList = http_build_query($params, null, '&');
			$opt[CURLOPT_URL] = $url . '?' . $queryList;
		}
		curl_setopt_array($ch, $opt);
		$contents = curl_exec($ch);
		//$return_code = curl_getinfo($ch, CURLINFO_HTTP_CODE);
		curl_close($ch);
		//if ($return_code == 200) return $contents;
		//else return NULL;
		return $contents;
	}
	
	/**
	 * 
	 * curl并发访问资源
	 * @param $resources   array(array('url'=>'','params'=''))     并发访问的资源数组
	 * @param $method	   String    curl请求方式，默认POST
	 * @param $timeout     int       设置curl响应超时时间
	 */
	public static function makeMultiRequest($resources){
		
		$queue = curl_multi_init();
		$map = array();
		
		foreach ($resources as $source) {
			$url = $source['url'];
			$method = $source['method'];
			$params = $source['params'];
			$flag = array_key_exists('flag', $source) ? $source['flag'] : $url;
			
			$is_ssl = substr($url, 0, 8) == 'https://' ? TRUE : FALSE;
			
			$ch = curl_init();
			
			$opt = array();
			$opt[CURLOPT_HEADER] = FALSE;
			$opt[CURLOPT_RETURNTRANSFER] = TRUE;
			$opt[CURLOPT_TIMEOUT] = self::DEFAULT_TIMEOUT;
			if ($is_ssl){
				$opt[CURLOPT_SSLCERT] = SSL_FILE_PATH;						//证书路径，在zwcenter/inc/config.php中配置
				$opt[CURLOPT_SSLKEYPASSWD] = SSL_PASSWORD;		//证书密码，在zwcenter/inc/config.php中配置
				$opt[CURLOPT_SSL_VERIFYPEER] = FALSE;
				$opt[CURLOPT_SSL_VERIFYHOST] = FALSE;
			}
			if ($method == 'POST'){
				$opt[CURLOPT_POST] = TRUE;
				$opt[CURLOPT_POSTFIELDS] = $params;
				$opt[CURLOPT_URL] = $url;
			}else{
				$queryList = http_build_query($params, null, '&');
				$opt[CURLOPT_URL] = $url . '?' . $queryList;
			}
			curl_setopt_array($ch, $opt);
			
			curl_multi_add_handle($queue, $ch);
			$map[(string) $ch] = $flag;
		}
		
		$responses = array();
		do {
			while (($code = curl_multi_exec($queue, $active)) == CURLM_CALL_MULTI_PERFORM) ;
			
			if ($code != CURLM_OK) {
				break; 
			}
			
			// 获取当前解析的cURL的相关传输信息
			while (@$done = curl_multi_info_read($queue)) {
				//获取一个cURL连接资源句柄的信息
				$source_flag = $map[(string) $done['handle']];
				$info = curl_getinfo($done['handle']);
				$error = curl_error($done['handle']);
				$content = curl_multi_getcontent($done['handle']);
				
				$responses[] = compact('source_flag','info', 'error', 'content');
				
				// 移除curl批处理句柄资源中的已处理句柄资源
				curl_multi_remove_handle($queue, $done['handle']);
				curl_close($done['handle']);
			}
			
			//判断栈中是否还有需要处理的cURL句柄
			if ($active > 0) {
				//阻塞直到cURL批处理连接中有活动连接
				curl_multi_select($queue, 1);
			}
		
		} while ($active);
		
		curl_multi_close($queue);
		return $responses;
		
	}
	
	
}